/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.logictics;

import aptech.eproject.logictics.dao.an.*;
import aptech.eproject.logictics.dao.kien.*;
import aptech.eproject.logictics.dao.khoa.*;
import aptech.eproject.logictics.dao.thinh.*;
import aptech.eproject.logictics.dao.thinh.ManageTrip.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author HOANGTHINH
 */
public class Util {

    // <editor-fold defaultstate="collapsed" desc="Util Methods of Thinh">
    public static RouteManageRemote lookupRouteService() {
        try {
            Context c = new InitialContext();
            return (RouteManageRemote) c.lookup("stateless/ManageRouteJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static DeliveryStatusManageRemote lookupDeliveryStatusService() {
        try {
            Context c = new InitialContext();
            return (DeliveryStatusManageRemote) c.lookup("stateless/ManageDeliveryStatusJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static AddTripRemote lookupAddTripService() {
        try {
            Context c = new InitialContext();
            return (AddTripRemote) c.lookup("stateful/AddTripJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static ManageTripRemote lookupManageTripService() {
        try {
            Context c = new InitialContext();
            return (ManageTripRemote) c.lookup("stateful/ManageTripJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static String castTime(Timestamp ts) {
        if (ts == null) {
            return "";
        } else {
            java.text.DateFormat format = new java.text.SimpleDateFormat("dd/MM/yyyy");
            return format.format(new Date(ts.getTime()));
        }
    }

    public static Date castDate(Timestamp ts) {
        if (ts == null) {
            return null;
        } else {
            java.text.DateFormat format = new java.text.SimpleDateFormat("dd/MM/yyyy");
            return new Date(ts.getTime());
        }
    }

    public static ReportServiceRemote lookupReportService() {
        try {
            Context c = new InitialContext();
            return (ReportServiceRemote) c.lookup("stateless/ReportServiceJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Util Methods of Kien">
    public static LoginServiceRemote lookupLoginService() {
        try {
            Context c = new InitialContext();
            return (LoginServiceRemote) c.lookup("stateless/LoginServiceJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static BookingManagementServiceRemote lookupBookingManagementService() {
        try {
            Context c = new InitialContext();
            return (BookingManagementServiceRemote) c.lookup("stateless/BookingManagementServiceJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static AccountServiceRemote lookupAccountService() {
        try {
            Context c = new InitialContext();
            return (AccountServiceRemote) c.lookup("stateless/AccountServiceJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static VehicleServiceRemote lookupVehicleService() {
        try {
            Context c = new InitialContext();
            return (VehicleServiceRemote) c.lookup("stateless/VehicleServiceJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static DeliveryServiceRemote lookupDeliveryService() {
        try {
            Context c = new InitialContext();
            return (DeliveryServiceRemote) c.lookup("stateless/DeliveryServiceJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }
    
    public static BranchServiceRemote lookupBranchService() {
        try {
            Context c = new InitialContext();
            return (BranchServiceRemote) c.lookup("stateless/BranchServiceJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    /**
     * Validate a credit card number
     * @param n
     *  Credit card number
     * @return
     *  true: Credit card is valid
     *  false: Credit card is invalid
     */
    public static boolean validCCNumber(String n) {
        try {
            /*
             ** known as the LUHN Formula (mod10)
             */
            int j = n.length();

            String[] s1 = new String[j];
            for (int i = 0; i < n.length(); i++) {
                s1[i] = "" + n.charAt(i);
            }

            int checksum = 0;

            for (int i = s1.length - 1; i >= 0; i -= 2) {
                int k = 0;

                if (i > 0) {
                    k = Integer.valueOf(s1[i - 1]).intValue() * 2;
                    if (k > 9) {
                        String s = "" + k;
                        k = Integer.valueOf(s.substring(0, 1)).intValue()
                                + Integer.valueOf(s.substring(1)).intValue();
                    }
                    checksum += Integer.valueOf(s1[i]).intValue() + k;
                } else {
                    checksum += Integer.valueOf(s1[0]).intValue();
                }
            }
            return ((checksum % 10) == 0);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Util Methods of Khoa">
    public static RegisterRemote lookupRegisterRemote() {
        try {
            Context c = new InitialContext();
            return (RegisterRemote) c.lookup("stateless/RegisterJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static ViewCustomerListRemote lookupViewCustomerListRemote() {
        try {
            Context c = new InitialContext();
            return (ViewCustomerListRemote) c.lookup("stateless/ViewCustomerListJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }

    public static UpdateProfileRemote lookupUpdateProfileRemote() {
        try {
            Context c = new InitialContext();
            return (UpdateProfileRemote) c.lookup("stateless/UpdateProfileJNDI");
        } catch (NamingException ne) {
            System.err.println("Namming not found ...");
            throw new RuntimeException(ne);
        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Util Methods of An">
    /**
     * Format date as dd/MM/yyyy
     * @param t
     *  Timestamp, the date object
     * @return
     *  a string represent for the date in format dd/MM/yyyy
     */
    public static String getDayString(Timestamp t) {
        if (t == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(t);
    }

    /**
     * Get current date in format dd/MM/yyyy
     * @return
     *  A string represent current date
     */
    public static String getCurrentDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(new Date());
    }

    /**
     * Format decimal number as #.##
     * @param dec
     *      Number of type BigDecimal to be formated
     * @return
     *      A string represent the number in #.## format
     */
    public static String getNumberFormat(BigDecimal dec) {
        DecimalFormat decF = new DecimalFormat("#.##");
        return decF.format(dec.doubleValue());
    }

    /**
     * Canculate number of days counting from now
     * @param t
     *  the date to be compared
     * @return
     *  number of days between now and the inputed date
     */
    public static double calculateDaysDiff(Timestamp t) {
        if (t==null) return 0;
        Calendar now = Calendar.getInstance();
        long mil = 24 * 3600 * 1000;
        double days = ((double) now.getTimeInMillis() - t.getTime()) / mil;
        return days;
    }

    public static BookingHistoryManageRemote lookupBookingHistoryManageRemote() {
        try {
            Context c = new InitialContext();
            return (BookingHistoryManageRemote) c.lookup("stateless/ManageBookingHistoryJNDI");
        } catch (NamingException ne) {
            //Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public static BranchServiceRemote lookupBranchServiceRemote() {
        try {
            Context c = new InitialContext();
            return (BranchServiceRemote) c.lookup("stateless/BranchServiceJNDI");
        } catch (NamingException ne) {
            //Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    // </editor-fold>
}
